//
//  IpadPlayerViewController.m
//  kankanVideo
//
//  Created by wangbo on 7/19/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

@interface NSString (md5extension)
- (NSString *) md5;
@end

@implementation NSString (md5extension)
- (NSString *) md5
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3], 
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];  
}
@end

#import "IpadPlayerViewController.h"
#import "GANTracker.h"

@interface IpadPlayerViewController ()

@end

@implementation IpadPlayerViewController
@synthesize videoList,lblTitle,movieplayerView=_movieplayerView,lblVideoTitle;


-(IBAction)back:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}
-(id)init{
    self=[super init];
    loadVideo=NO;
    videoList=[[VideoList alloc]init];
    [videoList.tableView setFrame:CGRectMake(0, 60, 332, 520)];
    videoList.delegate=self;
    //playerViewController=[[MyPlayerViewController alloc]init];
    [self.view addSubview:videoList.tableView];
    [videoList.tableView release];
    return self;
}

-(IBAction)btnrefresh:(id)sender{
    [videoList loaddata:clsid needRefresh:YES];
}

-(void)playFirstVideo:(NSMutableArray *)theParam{
    //NSLog(@"first_video:%@",_videourl);
    if (loadVideo) {
        //[self playMovieWithURL:_videourl withTitle:_title];
        [self playMovieWithParam:theParam];
        loadVideo=NO;
    }
}

-(void)playMovieWithParam:(NSMutableArray *)theParam{
    //lblTitle.text=[theParam objectAtIndex:0];
    lblVideoTitle.text=[theParam objectAtIndex:0];
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app showAlert:@"正在加载"];
    //NSString *str=[NSString stringWithFormat:@"http://interface.kankanews.com/kkapi/mobile/mobileapi.php?m=vcontent&url=%@",[theParam objectAtIndex:1]];
    NSString *str1=[[[[NSString stringWithFormat:@"api_key=mobile&m=vcontent&url=%@532c28d5412dd75bf975fb951c740a30",[theParam objectAtIndex:1]]stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"]stringByReplacingOccurrencesOfString:@":" withString:@"%3A"]md5];
    NSString *str=[NSString stringWithFormat:@"http://interface.kankanews.com/kkapi/mobile/mobileapin.php?api_key=mobile&access_token=%@&m=vcontent&url=%@",str1,[theParam objectAtIndex:1]];
    NSLog(@"%@",str);
    [[MyRequest shareInstance]loaddataWithURL:str forObject:self];
}

-(void)requestFinishedHandler:(ASIHTTPRequest*)request{
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app hideAlert];
    if(request.responseStatusCode==200){
        NSString *_url=[[[XMLParser parseVideoDetail:request.responseData]objectAtIndex:0]objectAtIndex:0];
        [[MPMoviePlayerView shareMoviePlayer]stop];
        NSString *videoLink=[NSString stringWithString:_url];
        NSURL *url = [NSURL URLWithString:videoLink];
        [self.movieplayerView playMovieAtURL:url];
        [[MPMoviePlayerView shareMoviePlayer] play];
    }
}

-(void)shareVideo:(NSString *)pageurl{
    _pageurl=pageurl;
    if ([[SinaController shareInstance]isLogin]) {
        [[SinaController shareInstance]sendWithContent:_pageurl withImage:nil];
        AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
        [app showAlert:@"分享成功"];
        [self performSelector:@selector(alertover) withObject:nil afterDelay:3];
    }else{
        [[SinaController shareInstance] setDelegate:self];
        [[SinaController shareInstance] login:self];
    }
}

-(void)didLogin{
    [[SinaController shareInstance]sendWithContent:_pageurl withImage:nil];
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app showAlert:@"分享成功"];
    [self performSelector:@selector(alertover) withObject:nil afterDelay:3];
}

-(void)alertover{
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app hideAlert];
}

-(void)showVideoList:(NSMutableArray *)param{
    NSLog(@"showVideoList");
    lblTitle.text=[param objectAtIndex:1];
    clsid=[param objectAtIndex:0];
    loadVideo=YES;
    
    //update and load data in videolist panel
    [videoList loaddata:clsid needRefresh:NO];
    
    //update categorylist panel
    [self tidyCetagoryList];
}

-(void)tidyCetagoryList{
    NSArray *arr=[[MyCoreData shareInstance]query];
    int cc=[arr count];
    if(cc==0){
        AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
        [app showAlert:@"正在加载"];
        return;
    }
    for (ListItem *item in myOrderScrollView.subviews) {
        if ([item isKindOfClass:[ListItem class]]) {
            [item removeFromSuperview];
        }
    }
    
    /*for (int k=0; k<10; k++) {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.titleLabel.text=@"xiaowu";
        [myOrderScrollView addSubview:btn];
        NSLog(@"myOrderScrollView_count:%d",myOrderScrollView.subviews.count);
    }*/
    
    
    int j=0;
    for(int i=0;i<cc;i++){
        IndexData *obj=[arr objectAtIndex:i];
        if (![obj.classid isEqualToString:clsid]) {
            NSMutableArray *barray=[NSMutableArray arrayWithCapacity:5];
            [barray addObject:[obj classid]];
            [barray addObject:[obj title]];
            [barray addObject:[obj titlepic]];
            [barray addObject:[obj bigpic]];
            [barray addObject:[obj isgood]];
            ListItem *item=[[ListItem alloc]initWithFrame:CGRectMake(j*150,24, 120, 120) withParam:barray];
            //ListItem *item=[[ListItem alloc]initWithFrame:CGRectMake(0,0, nw, nh) withImageURL:[obj titlepic]];
            item.delegate=self;
            
            NSLog(@"%@",item);
            
            [myOrderScrollView addSubview:item];
            
            /*UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn.titleLabel.text=@"xiaowu";
            [myOrderScrollView addSubview:btn];*/
            
            
            
            NSLog(@"%d",myOrderScrollView.subviews.count);
            
            [item release];
            j++;
        }
        //NSLog(@"iiiittteeemm_:%d",item.retainCount);
    }
    [myOrderScrollView setContentSize:CGSizeMake(150*j, 168)];
    //itemcount=cc;
}

-(void)showVideoList:(NSString *)classid withTitle:(NSString *)theTitle{
    [self showVideoList:[NSMutableArray arrayWithObjects:classid,theTitle, nil]];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillAppear:(BOOL)animated{
    NSError *error;
    if (![[GANTracker sharedTracker] trackPageview:@"看看吧ipad-列表点播页"
                                         withError:&error]) {
        NSLog(@"error in trackPageview");
    }
    NSLog(@"guilder_view_will_appear");
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[MPMoviePlayerView shareMoviePlayer] stop];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight||interfaceOrientation==UIInterfaceOrientationLandscapeLeft);
}

@end
